package com.example.ftptest;

import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class LoginActivity extends Activity {
	
	private static final String TAG = "MainActivity";
	private static final String TEMP_FILENAME = "test.txt";
	private Donnee donnee;
	
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);		
		
		Button button1 = (Button) findViewById(R.id.button1);
			
		button1.setOnClickListener(mClick);

		createDummyFile();
		
	
	}
	OnClickListener mClick = new OnClickListener() {
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		EditText editHost = (EditText)findViewById(R.id.editText4);
		String Host = editHost.getText().toString();	
		EditText editPort = (EditText)findViewById(R.id.editText2);
		int Port = Integer.parseInt(editPort.getText().toString());		
		EditText editUser = (EditText)findViewById(R.id.editText3);
		String User = editUser.getText().toString();		
		EditText editPass = (EditText)findViewById(R.id.editText1);
		String Pass = editPass.getText().toString();
			
		Log.d(TAG,"valeur de host : " +Host);
		Log.d(TAG,"valeur de Port : " +Port);
		Log.d(TAG,"valeur de User : " +User);
		Log.d(TAG,"valeur de Pass : " +Pass);
		login(Host,User,Pass,Port);
				
			
		}
	};
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.login, menu);
		return true;
	}
	
	
	public void login(final String Host,final String User, final String Pass,final int Port) {
		new Thread(new Runnable() {
			public void run(){
			MyFTPClient ftpclient = new MyFTPClient();
			boolean status = false;
			Log.d(TAG, "passage avant connexion");
			status = ftpclient.ftpConnect(Host, User, Pass, Port);
			Log.d(TAG,"valeur de host : " +Host);
//			ftp.drivehq.com", "FTP_UID", "FTP_PW", 21
			Log.d(TAG,"recuperation des H,P,U,P");
			if (status == true) {
				Log.d(TAG,"connexion reussi");
				Log.d(TAG, "Connection Success");
				donnee = new Donnee();
				donnee.setHOST(Host);
				donnee.setPASS(Pass);
				donnee.setPORT(Port);
				donnee.setUSER(User);
				menuFTP(ftpclient);

			} else {
				Log.d(TAG,"connexion echoue");
				//Toast.makeText(getApplicationContext(), "Connection failed", 2000).show();
				Log.d(TAG, "Connection failed");
			}
			}
		}).start();
	
	
	}
	
	protected void menuFTP(MyFTPClient ftpclient) {
		try {
			ftpclient.mFTPClient.disconnect();
			Log.d(TAG,"DECONNECTION OK");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.d(TAG,"DECONNECTION ECHOUE");
		}
		Intent menuIntent = new Intent(this,MenuActivity.class);
		menuIntent.putExtra("host", donnee.getHOST());
		menuIntent.putExtra("pass", donnee.getPASS());
		menuIntent.putExtra("port", donnee.getPORT());
		menuIntent.putExtra("user", donnee.getUSER());
		startActivity(menuIntent);
	}
	
	public void createDummyFile() {
		try {
			FileOutputStream fos;
			String file_content = "RODRIGUE ET ZINSE , VOICI LE FRUIT D'UN TRAVAIL FAIT !!! AU LIEU DE DORMIR EN COURS";
			
			fos = openFileOutput(TEMP_FILENAME, MODE_PRIVATE);
			fos.write(file_content.getBytes());
			fos.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}